<?php 
abstract class AdminMaster extends sfAction{
	public $data   = null;
	public $formData = array();
	public $formFilter = array(
				'filter_key' 		=> '',
				'filter_enabled' 	=> '-1',
				'filter_submit'		=> ''
			);
	public function preExecute(){
		sfContext::getInstance()->getConfiguration()->loadHelpers('I18N');
		
		$account = $this->getUser()->getAttribute('admin.accounts',array());
		if(!is_array($account) ||  count($account)!=3 || empty($account['login_user']) || intval($account['id'])<=0 || intval($account['admin_type'])<0 ||intval($account['admin_type'])>2 ){
			$this->redirect( sfConfig::get('app_prefix') . $this->generateUrl('home_login'));
		}
		
		/*checking permission module*/
		$isPermission = false;
		$menuJson 	= json_decode(file_get_contents(sfConfig::get('sf_web_dir').'/../apps/admin/config/admin.json'),true);
		foreach ($menuJson as $module){
			foreach($module['actions'] as $key=>$action){
				if($action['name'] == '/'.$this->getModuleName().'/'.$this->getActionName()){
					$isPermission = SysAccounts::checkPermission($action);
				}
			}
		}
		if(!$isPermission){
			foreach ($menuJson as $module){
				if($module['name']=='allowAction'){
					$isPermission = SysAccounts::checkAjaxPermission($module, $this->getModuleName().'/'.$this->getActionName());
				}
			}
		}
		if(!$isPermission && $this->getModuleName() !='preview' && $this->getModuleName() !='user' && $this->getModuleName() !='uploadControl'){
			$this->redirect( sfConfig::get('app_prefix') . $this->generateUrl('home_login'));
		}
		/*End */
		$request = $this->getRequest();
		$this->data['filter'] 		= $request->getParameter('filter');
		if(empty($this->data['filter']) && !is_array($this->data['filter'])){		//if data is empty (or not filter post), get data from url
			foreach($this->formFilter as $key => $val){
				$this->data['filter'][$key] = $request->getParameter($key, $val);
			}
		}
		
		$this->data['action'] 		= $request->getParameter('action_name');		//get action
		$this->data['is_post']		= $request->isMethod('POST');					//checking post
		$this->data['select_list']	= $request->getParameter('select_list');		//get array of id selected
		$this->data['id']			= $request->getParameter('id', -1);				//get id for edit data
		$this->data['app_prefix']	= sfConfig::get('app_prefix');
		$this->data['redirect'] 	= sfConfig::get('app_prefix').'/'.$this->getUser()->getCulture().'/'.$this->getModuleName().'/'.$this->getActionName();
		$this->data['ids']			= $request->getParameter('ids',null);
		$this->data['sort_orders']	= $request->getParameter('sort_orders',null);
		$this->data['ajax_value']	= $request->getParameter('val',null);
		$this->data['is_alert']		= 0;
		
		$this->data['pagination']	= array(
					'page'		=> $request->getParameter('page', 1),
					'limit'		=> $request->getParameter('limit', sfConfig::get('app_pagination_limit')),
					'page_show'	=> sfConfig::get('app_pagination_page_show')
				);
			
		$this->data['filter_action']	= $this->generalAction();
		
	}
	private function generalAction(){
		$action = '?';
		if(is_array($this->data['filter'])){
			foreach($this->data['filter'] as $key => $val){
				$action.='&'.$key.'='.urlencode($val);
			}
		}
		return $action;
	}
}